package com.evitch.csvtrade;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class SynchroFile implements StrategyRunner {

	private String commandfilename = "commandfile.txt";
	private String resultfilename = "resultfile.txt";
	private String path = "C:\\Program Files\\MetaTrader - ActivTrades\\experts\\files\\";
	private PrintWriter writer;
	private File cmdf = new File(path + commandfilename);
	private File resf = new File(path + resultfilename);

	public SynchroFile() {
		if(cmdf.exists()){
			cmdf.delete();
			try {
				cmdf.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(resf.exists()){
			resf.delete();
			try {
				resf.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void open() {
		try {
			writer = new PrintWriter(new BufferedWriter(new FileWriter(cmdf)));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void close() {
		writer.close();
	}

	public void write(String line) {
		writer.println(line);
	}

	public boolean exist() {
		return cmdf.exists();
	}

	public String readline() {
		InputStream ips;
		String line = null;
		try {
			ips = new FileInputStream(resf);

			InputStreamReader ipsr = new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);

			try {
				line = br.readLine();
				br.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return line;
	}

	public String getResult(long key) {
		String res = readline();
		boolean isRes = false;
		if (res != null) {
			int idx = res.indexOf(";");
			String keystr =  res.substring(0,idx);
			if(keystr.equals(String.valueOf(key))){
				isRes = true;	
				res = res.substring(idx+1);
				//System.out.println("result "+res);
			}
		}
		if(!isRes){
			res = "";
		}
		
		return res;
	}

	@Override
	public void start(String paramString) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isAlive() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void resCommand(String paramString) {
		// TODO Auto-generated method stub

	}

	@Override
	public String getCommand() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String sendCommandGetResult(StringBuilder paramStringBuilder) {
		//System.out.println(paramStringBuilder);
		long key = getUniqKey();
		open();
		write(constructRequest(key, paramStringBuilder));
		close();
		String res = "";
		while(res.equals("")){ 
			res =  getResult(key); 
		} 
		return res;
	}

	public String constructRequest(long key, StringBuilder paramStringBuilder) {
		//System.out.println(paramStringBuilder);
		int firstspace = paramStringBuilder.indexOf(" ");
		String cmdNum = paramStringBuilder.substring(0, firstspace);
		//System.out.println("cmdnum :" + cmdNum);
		int secondspace = paramStringBuilder.indexOf(" ", firstspace + 1);
		String cmdStr = "";
		String args = "";
		if (secondspace != -1) {
			cmdStr = paramStringBuilder.substring(firstspace + 1, secondspace);
			args = paramStringBuilder.substring(secondspace + 1);
		} else {
			cmdStr = paramStringBuilder.substring(firstspace + 1);
		}
		//System.out.println("cmd :" + cmdStr);
		//System.out.println("args :" + args);
		boolean end = false;
		String argsStr = "";
		if (!args.equals("")) {
			while (!end) {
				argsStr += ";";
				String arg = "";
				//System.out.println("args"+args);
				int spidx = args.indexOf(" ");
				//System.out.println("spidx"+spidx);		
				
				if (spidx != -1) {
					arg = args.substring(0, spidx);
					args = args.substring(spidx + 1);
				} else {
					arg = args;
					end = true;
				}
				//System.out.println("arg"+arg);
				argsStr += arg.substring(1, arg.length() - 1);

			}
		}
		//System.out.println(key+";"+cmdNum+argsStr);
		return key+";"+cmdNum+argsStr;
	}

	public long getUniqKey() {
		return System.nanoTime();
	}
}
